const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    custSourceListData: app.globalData.custSourceListData,
    custStatuListData: app.globalData.custStatuListData,
    yearListData: app.globalData.yearListData,
    mileageListData: app.globalData.mileageListData,
    colorListData: app.globalData.colorListData,
    cityListData: ['上海', '北京', '深圳', '杭州'],
    index: 0,
    chosen: '',
    car_model:"+",
    carType: 0
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 表单提交事件
   */
  submitCustInfo: function(e) {
    var data = app.globalData.request_common_para

    var city = this.data.cityListData[this.data.index]
    var cust = e.detail.value

    var cust_name = cust.cust_name //客户姓名
    if (!cust_name) {
      app.noTitlemodalTap("请输入客户姓名")
      return;
    }
    var cust_tel = cust.cust_tel //客户电话
    if (!cust_tel) {
      app.noTitlemodalTap("请输入客户手机")
      return;
    }
    var car_source = cust.car_source //客户来源
    if (!car_source) {
      app.noTitlemodalTap("请选择客户来源")
      return;
    }

    var cust_status = cust.cust_status //购车意向
    if (!cust_status) {
      app.noTitlemodalTap("请选择意向等级")
      return;
    }
    var begin_price = cust.begin_price //价格
    var end_price = cust.end_price
    if (!begin_price || !end_price) {
      app.noTitlemodalTap("请输入预购车算")
      return;
    }

    var car_model = cust.car_model //车型
    if (!car_model) {
      app.noTitlemodalTap("请输入车型")
      return;
    }
    var car_year_list = cust.year_list //年限
    var car_year_val = "";
    for (var i in car_year_list) {
      car_year_val += car_year_list[i]
      if (i != car_year_list.length - 1) {
        car_year_val += "|"
      }
    }
    if (!car_year_val) {
      app.noTitlemodalTap("请选择年限要求")
      return;
    }
    var car_mileage_list = cust.mileage_list //里程
    var car_mileage_val = ""
    for (var i in car_mileage_list) {
      car_mileage_val += car_mileage_list[i];
      if (i != car_mileage_list.length - 1) {
        car_mileage_val += "|"
      }
    }
    if (!car_mileage_val) {
      app.noTitlemodalTap("请选择里程要求")
      return;
    }

    var car_color_list = cust.car_color //颜色
    var car_color_val = "";
    for (var i in car_color_list) {
      car_color_val += car_color_list[i]
      if (i != car_color_list.length - 1) {
        car_color_val += "|"
      }
    }
    if (!car_color_val) {
      app.noTitlemodalTap("请选择颜色要求")
      return;
    }

    var car_no_region = cust.car_no_region //落地户
    var remark = cust.remark //备注
    var jianyi = cust.jianyi //建议
    var other_car_no_region = cust.other_car_no_region //外地车牌
    if (!car_no_region) {
      app.noTitlemodalTap("请选择落地户")
      return;
    }
    app.toast3Tap()

    data.audit = 0
    data.budgetLimitMax = end_price
    data.budgetLimitMin = begin_price
    data.carModel = car_model
    data.city = city
    data.content = remark
    data.count = 0
    data.customerName = cust_name
    data.customerPhone = cust_tel
    data.customerSource = car_source
    data.note = jianyi
    data.requirementColor = car_color_val
    data.requirementMileage = car_mileage_val
    data.requirementSettled_in = other_car_no_region
    data.requirementYear = car_year_val
    data.state_name = cust_status
    data.sumParam = 0
    console.info(data)
    wx.request({
      url: 'https://mcc.mtmche.cn/datalake/v1/userManages',
      data: data,
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
		  console.info("创建客户：" + JSON.stringify(res.data))
        if (res.statusCode == 200) {
          app.toast1Tap()
          console.info("创建客户：" + JSON.stringify(res.data))
          wx.navigateTo({
            url: 'saveCust',
          })
        }
      },
      complete: function(res) {},
      fail: function(res) {
        console.error(res);
      }
    })
  },
  bindinputCarModel:function(e){
    console.info(e)
    wx.navigateTo({
      url: 'carModelType?selectedCarModelType=' + this.data.car_model + '&carType=' + this.data.carType
    })
  }
})